package xyz.zhangyi.onjava.java8.example.stream.model;

import java.util.Arrays;
import java.util.List;

public class Transaction {
    private Trader trader;
    private int year;
    private int value;

    public Transaction(Trader trader, int year, int value) {
        this.trader = trader;
        this.year = year;
        this.value = value;
    }

    public Trader getTrader() {
        return this.trader;
    }

    public int getYear() {
        return this.year;
    }

    public int getValue() {
        return this.value;
    }

    public String cityOfTrader() {
        return this.trader.getCity();
    }

    /*
        Quiz 1: Find all transactions from year 2011 and sort them by value (small to high).
        Quiz 2: What are all the unique cities where the traders work?
        Quiz 3: Find all traders from Beijing and sort them by name.
        Quiz 4: Return a string of all traders’ names sorted alphabetically.
        Quiz 5: Are there any trader based in Shanghai?
        Quiz 6: Update all transactions so that the traders from Shanghai to Beijing.
        Quiz 7: What's the highest value in all the transactions?
     */
    public static List<Transaction> generate() {
        Trader liuBei = new Trader("Liu Bei", "Beijing");
        Trader caoCao = new Trader("Cao Cao", "Shanghai");
        Trader guanYu = new Trader("Guan Yu", "Beijing");
        Trader zhangFei = new Trader("Zhang Fei", "Beijing");

        return Arrays.asList(
                new Transaction(zhangFei, 2011, 300),
                new Transaction(liuBei, 2012, 1000),
                new Transaction(liuBei, 2011, 400),
                new Transaction(caoCao, 2012, 710),
                new Transaction(caoCao, 2012, 700),
                new Transaction(guanYu, 2012, 950)
        );
    }

    public String toString() {
        return "{" + this.trader + ", " +
                "year: " + this.year + ", " +
                "value:" + this.value + "}";
    }
}